package com.cn.wanxi.data.impl;

import com.cn.wanxi.data.UserData;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserData {
    @Override
    public int add(UserModel userModel) {
//        String sql = "INSERT INTO `sys`.`sys_user` \n" +
//                "( `username`," +
//                " `password`," +
//                " `phone`," +
//                " `weight`," +
//                " `email`," +
//                " `sex`," +
//                " `hobby`," +
//                " `birthday`," +
//                "`create_time`)\n" +
//                " VALUES ( '" + userModel.getUsername() + "'," +
//                " '" + userModel.getPassword() + "'," +
//                " '" + userModel.getPhone() + "'," +
//                " '" + userModel.getWeight() + "'," +
//                " '" + userModel.getEmail() + "'," +
//                " '" + userModel.getSex() + "'," +
//                " '" + userModel.getHobby() + "'," +
//                " '" + userModel.getBirthday() + "'," +
//                "now())";


        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.data.UserData.add", userModel);

//1.13.4
    }

    @Override
    public boolean findByUsername(String username) {
//        String sql = "select * from sys.sys_user where username='" + username + "'";
//        ResultSet resultSet = JDBC.query(sql);
//        UserModel userModel = null;
//
//        try {
//            while (resultSet.next()) {
//                userModel = new UserModel();
//
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.data.UserData.findByUsername") != null;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
//        String sql = "select * from sys_user " +
//                "where username='" + userModel.getUsername() + "' and " +
//                "password='" + userModel.getPassword() + "'";
//        ResultSet resultSet = JDBC.query(sql);
//        UserModel model = null;
//
//        try {
//            while (resultSet.next()) {
//                model = new UserModel();
//
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.data.UserData.findByUsernameAndPassword", userModel);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
//        String sql = "select * from sys_user " +
//                "where true ";
//        if (userModel.getUsername() != null && !"".equals(userModel.getUsername())) {
//            sql += "and username like '%" + userModel.getUsername() + "%' ";
//        }
//        if (userModel.getPhone() != null && !"".equals(userModel.getPhone())) {
//            sql += "and phone like '%" + userModel.getPhone() + "%'";
//        }
//        if (userModel.getEmail() != null && !"".equals(userModel.getEmail())) {
//            sql += "and email like '%" + userModel.getEmail() + "%'";
//        }
//        if (!"".equals(userModel.getHobby())) {
//            String[] hobbys = userModel.getHobby().split(",");
//            for (int i = 0; i < hobbys.length; i++) {
//                sql += " and FIND_IN_SET('" + hobbys[i] + "',hobby)";
//            }
//        }
////        if (userModel.getHobby() != null && !"".equals(userModel.getHobby())) {
////            sql += "and hobby like '%" + userModel.getHobby() + "%'";
////        }
//        if (userModel.getSex() != null && !"全部".equals(userModel.getSex())) {
//            sql += "and sex like '%" + userModel.getSex() + "%'";
//        }
//        if (!"".equals(userModel.getStartBirthday()) && "".equals(userModel.getEndBirthday())) {
//            sql += " and birthday >='" + userModel.getStartBirthday() + "'";
//        }
//        if ("".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
//            sql += " and birthday <='" + userModel.getEndBirthday() + "'";
//        }
//        if (!"".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
//            sql += " and birthday between '" + userModel.getStartBirthday() + "' and '" + userModel.getEndBirthday() + "'";
//        }
////        if (userModel.getEnable() != -1 && userModel.getEnable() != null){
////            if (userModel.getEnable() == 1){
////                sql +=" and enable=1";
////            }else{
////                sql +="and enable!=1";
////            }
////        }
//        sql += "order by id desc";
//        ResultSet resultSet = JDBC.query(sql);
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.data.UserData.findAll",userModel);
    }

    @Override
    public int del(Integer id) {
//        String sql = "delete from sys_user where id=" + id;

        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.data.UserData.del", id);
    }

    @Override
    public UserModel findById(Integer id) {
//        String sql = "select * from sys_user where id=" + id;
//        ResultSet resultSet = JDBC.query(sql);
//        List<UserModel> list = setResultData(resultSet);
        List<UserModel> list=GetSqlSession.getSqlSession().selectList("com.cn.wanxi.data.UserData.findById", id);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int edit(UserModel userModel) {
        String sql = "update user set username='" + userModel.getUsername() + "'where id =" + userModel.getId();
        return JDBC.excute(sql);
    }

    @Override
    public UserModel findByUsernameForEdit(UserModel userModel) {
        String sql = "select * from sys_user where username='" + userModel.getUsername() + "'and id !=" + userModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = setResultData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    //放置数据到sql方法
    private List<UserModel> setResultData(ResultSet resultSet) {
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setEmail(resultSet.getString("email"));
                model.setHobby(resultSet.getString("hobby"));
                model.setSex(resultSet.getString("sex"));
                model.setEnable(resultSet.getInt("enable"));
                model.setBirthday(resultSet.getString("birthday"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}

